package com.wang.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.result.Result;
import com.wang.model.system.SysRole;
import com.wang.auth.service.SysRoleService;
import com.wang.model.system.vo.AssignRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/admin/system/sysRole")
@Api(tags = "角色管理接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll() {
        List<SysRole> list = sysRoleService.list();
//        try{
//            int i = 1/0;
//        }catch (Exception e){
//            throw new CustomException(2001,"执行自定义异常");
//        }
        return Result.success(list);
    }

    @ApiOperation("条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true),
//            @ApiImplicitParam(name = "sysRole", value = "条件", required = false)       //这句话不要，它的参数列表有SysRole的各个属性而没有SysRole这个类
            @ApiImplicitParam(name = "roleName", value = "条件", required = false),
    })
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    //当前用户中perms有这个值才能操作这个接口，没有这个值不能操作这个接口
    @GetMapping("/{page}/{pageSize}")
    //Get方式，在地址栏传SysRole的熟悉，自动封装到SysRole，不用加RequestBody
    public Result<Page<SysRole>> page(@PathVariable Integer page, @PathVariable Integer pageSize, SysRole sysRole){
        Page<SysRole> sysRolePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleLambdaQueryWrapper.like(Strings.isNotEmpty(sysRole.getRoleName()),SysRole::getRoleName,sysRole.getRoleName());
        sysRoleService.page(sysRolePage,sysRoleLambdaQueryWrapper);
        return Result.success(sysRolePage);
    }

    @ApiOperation("添加角色接口")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("/save")
    public Result<String> save(@RequestBody  SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if (save) {
            return Result.success(null,"添加成功");
        }
        return Result.error("添加失败");
    }

    @ApiOperation("根据id查询角色信息")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/get/{id}")
    public Result<SysRole> getById(@PathVariable Long id){
        return Result.success(sysRoleService.getById(id));
    }

    @ApiOperation("根据id修改角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PutMapping("/update")
    public Result<String> updateById(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.updateById(sysRole);
        if (b){
            return Result.success(null,"修改成功");
        }
        return Result.error("修改失败");
    }

    @ApiOperation("根据id删除角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/remove/{id}")
    public Result<String> deleteById(@PathVariable Long id){
        boolean b = sysRoleService.removeById(id);
        if (b) {
            return Result.success(null, "删除成功");
        }
        return Result.error("删除失败");
    }

    @ApiOperation("根据id批量删除")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/batchRemove")
    public Result<String> batchRemove(@RequestBody List<Long> idList){
        boolean b = sysRoleService.removeByIds(idList);
        if (b){
            return Result.success(null,"批量删除成功");
        }
        return Result.error("删除失败");
    }

    @ApiOperation("查询所有角色和当前用户所属角色")
    @GetMapping("/toAssign/{userId}")
    public Result<Map<String,Object>> toAssign(@PathVariable Long userId){
        Map<String,Object> map = sysRoleService.findRoleDataByUserId(userId);
        return Result.success(map);
    }

    @ApiOperation("根据用户id分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleVo assignRoleVo){
        sysRoleService.doAssign(assignRoleVo);
        return Result.success();
    }

}
